package com.dlxj.service.impl;

import java.util.List;

import com.dlxj.dao.BaseDAO;
import com.dlxj.entity.Bugtype;
import com.dlxj.entity.Group;
import com.dlxj.service.BugTypeService;
import com.dlxj.util.ConstClass;

public class BugTypeServiceImpl implements BugTypeService {

	BaseDAO baseDAO;

	@Override
	public int addBugType(Bugtype bugtype) {
		int count = baseDAO.countHql(
				"select count(*) from Bugtype where name = ?",
				new Object[] { bugtype.getName() });
		if (count > 0) {

			return ConstClass.ADD_FAILURE;
		}
		baseDAO.save(bugtype);
		return ConstClass.ADD_SUCCESS;
	}

	@Override
	public int deleteBugType(int bid) {
		int count = baseDAO.countHql(
				"select count(*) from Receiptpatrol where bid = ?",
				new Object[] { bid });
		if (count > 0) {
			return ConstClass.DELETE_FAILURE;
		}
		baseDAO.delete(Bugtype.class, bid);
		return ConstClass.DELETE_SUCCESS;

	}

	@Override
	public int modifyBugType(int bid, String name) {
		Bugtype bugtype = (Bugtype) baseDAO.findById(Bugtype.class, bid);
		if (bugtype == null) {
			return ConstClass.UPDATE_FAILURE;
		}
		int count = baseDAO.countHql(
				"select count(*) from Bugtype where name = ?",
				new Object[] { name });
		if (count > 0) {

			return ConstClass.UPDATE_FAILURE;
		}
		bugtype.setName(name);
		return ConstClass.ADD_SUCCESS;
	}

	@Override
	public int modifyBugTypeState(int bid, int state) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<Bugtype> findBugTypeList() {

		return (List<Bugtype>) baseDAO.findByHql("from Bugtype");
	}

	public BaseDAO getBaseDAO() {
		return baseDAO;
	}

	public void setBaseDAO(BaseDAO baseDAO) {
		this.baseDAO = baseDAO;
	}

	@Override
	public int findBugTypeById(int bid) {
		int count = baseDAO.countHql(
				"select count(*) from Receiptpatrol where bid = ?",
				new Object[] { bid });
		if (count > 0) {
			return 1;
		}
		return 0;
	}

}
